package org.cybzacg.convert.converters.date;

import lombok.extern.slf4j.Slf4j;
import org.cybzacg.convert.annotation.Converter;
import org.cybzacg.convert.context.ConversionContext;
import org.cybzacg.convert.enums.ConversionPriority;
import org.cybzacg.convert.enums.ConversionType;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * LocalTime转换器
 * 专门处理LocalTime类型的转换
 *
 * @author cybzacg.blog
 * @version 4.0
 * @since 3.0
 */
@Slf4j
@Converter(name = "LocalTimeConverter", priority = ConversionPriority.MEDIUM_HIGH, description = "LocalTime类型转换器，处理各种类型到java.time.LocalTime的转换", enabled = true, version = "4.0", author = "cybzacg.blog", supportedTypes = {
        LocalTime.class }, tags = { "localtime", "time", "conversion" })
public class LocalTimeConverter extends AbstractDateTimeConverter<LocalTime> {

    public LocalTimeConverter() {
        super(LocalTime.class, ConversionPriority.MEDIUM_HIGH);
    }

    @Override
    protected ConversionType getConversionType() {
        return ConversionType.LOCAL_TIME;
    }

    @Override
    protected LocalTime parseFromString(String str, DateTimeFormatter formatter) {
        return LocalTime.parse(str, formatter);
    }

    @Override
    protected LocalTime convertFromInstant(java.time.Instant instant, ConversionContext context) {
        return instant.atZone(DEFAULT_ZONE_ID).toLocalTime();
    }

    @Override
    protected String[] getSupportedPatterns() {
        return org.cybzacg.convert.constants.DateFormats.TIME_FORMAT_PATTERNS;
    }

    @Override
    protected LocalTime convertFromOtherDateTime(Object source, ConversionContext context) {
        // LocalDateTime转换
        if (source instanceof java.time.LocalDateTime localDateTime) {
            return localDateTime.toLocalTime();
        }

        // ZonedDateTime转换
        if (source instanceof java.time.ZonedDateTime zonedDateTime) {
            return zonedDateTime.toLocalTime();
        }

        return null;
    }

    @Override
    protected LocalTime getDefaultValue() {
        return org.cybzacg.convert.constants.DateTimeDefaults.LOCAL_TIME_DEFAULT;
    }
}
